﻿using System;
using System.Drawing;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace LevelEditor.Forms
{
    public partial class LayerForm : DockContent
    {
        #region Fields

        MainForm owner;

        #endregion

        #region Constructors

        public LayerForm(MainForm form)
        {
            InitializeComponent();
            owner = form;
        }

        #endregion

        #region Methods

        public void Reload()
        {
            Controls.Clear();
            LoadControls();
            Invalidate();
        }

        public void Reset()
        {
            foreach (Control c in Controls)
                (c as LayerDetailsControl).ResetControls(owner);
        }

        #endregion

        #region Helper Methods

        private void LoadControls()
        {
            for (int i = owner.Level.Layers.Count - 1; i >= 0; --i)
            {
                LayerDetailsControl ldc = new LayerDetailsControl(owner, i);
                ldc.MoveUpClicked += new LayerDetailsControl.MoveUpClickedEventHandler(ldc_MoveUpClicked);
                ldc.MoveDownClicked += new LayerDetailsControl.MoveDownClickedEventHandler(ldc_MoveDownClicked);
                ldc.WidthChanged += new LayerDetailsControl.WidthChangedEventHandler(ldc_WidthChanged);
                ldc.HeightChanged += new LayerDetailsControl.HeightChangedEventHandler(ldc_HeightChanged);
                ldc.OptionsClicked += new LayerDetailsControl.OptionsClickedEventHandler(ldc_OptionsClicked);
                ldc.VisibleClicked += new LayerDetailsControl.VisibleClickedEventHandler(ldc_VisibleClicked);
                ldc.PaletteClicked += new LayerDetailsControl.PaletteClickedEventHandler(ldc_PaletteClicked);
                ldc.Location = new Point(0, (owner.Level.Layers.Count - i - 1) * 75);
                Controls.Add(ldc);
            }
        }

        #endregion

        #region Events

        void ldc_PaletteClicked(object sender, EventArgs e)
        {
            owner.CurrentLayer = (sender as LayerDetailsControl).LayerIndex;
            owner.RefreshControlData();
        }

        void ldc_VisibleClicked(object sender, EventArgs e)
        {
            owner.Level.Layers[(sender as LayerDetailsControl).LayerIndex].Visible = !owner.Level.Layers[(sender as LayerDetailsControl).LayerIndex].Visible;
            (sender as LayerDetailsControl).UpdateVisibleImage(owner);
            owner.RefreshControlData();
        }

        void ldc_OptionsClicked(object sender, EventArgs e)
        {
            int previousLayerIndex = owner.CurrentLayer;
            owner.CurrentLayer = (sender as LayerDetailsControl).LayerIndex;
            owner.layerOptionsTSB_Click(sender, e);
            owner.CurrentLayer = previousLayerIndex;
        }

        void ldc_HeightChanged(object sender, EventArgs e)
        {
            owner.ResizeLayer((sender as LayerDetailsControl).LayerIndex,
                new Size(owner.Level.Layers[(sender as LayerDetailsControl).LayerIndex].Size.Width,
                    (sender as LayerDetailsControl).LayerHeight));
        }

        void ldc_WidthChanged(object sender, EventArgs e)
        {
            owner.ResizeLayer((sender as LayerDetailsControl).LayerIndex,
                new Size((sender as LayerDetailsControl).LayerWidth,
                owner.Level.Layers[(sender as LayerDetailsControl).LayerIndex].Size.Height));
        }

        void ldc_MoveDownClicked(object sender, EventArgs e)
        {
            (sender as LayerDetailsControl).LayerIndex = owner.MoveLayerDown((sender as LayerDetailsControl).LayerIndex);
        }

        void ldc_MoveUpClicked(object sender, EventArgs e)
        {
            (sender as LayerDetailsControl).LayerIndex = owner.MoveLayerUp((sender as LayerDetailsControl).LayerIndex);
        }

        private void LayerForm_Load(object sender, EventArgs e)
        {
            Location = new Point(owner.Location.X + owner.Width + 5, owner.Location.Y + owner.Height - Height);
            LoadControls();
        }

        private void LayerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private void addLayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            owner.addLayerTSB_Click(sender, e);
        }

        private void removeLayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            owner.removeLayerTSB_Click(sender, e);
        }

        private void editLayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            owner.layerOptionsTSB_Click(sender, e);
        }

        #endregion
    }
}
